package com.handu.luna.core.domain.article;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.handu.luna.core.domain.BaseRepository;



public interface ArticleCategoryRepository extends JpaRepository<ArticleCategory,Long>, BaseRepository<ArticleCategory>,JpaSpecificationExecutor<ArticleCategory>{

	@Query("from ArticleCategory c")
	List<ArticleCategory> findAll();   //b.inputValues like CONCAT('%',:inputValues,'%')
	
	//@Query("from ArticleCategory c where c.parentCollection like CONCAT('%',:id,'%')")
	//List<ArticleCategory> findAll(@Param("id") long id);
	
	@Query("from ArticleCategory c where c.parent_id= :parent_id order by sort_order asc")
	List<ArticleCategory> getParent(@Param("parent_id") Long parent_id);
	
	@Query("from ArticleCategory c  order by sort_order asc")
	List<ArticleCategory> getParents();
	
	@Query("from ArticleCategory c where c.parent_id= :parent_id and c.name =:name")
	List<ArticleCategory> findByCatNameAndPid(@Param("name") String name, @Param("parent_id") Long parent_id);
	
	@Query("from ArticleCategory c where c.id= :id")
	List<ArticleCategory> findById(@Param("id") Long id);
	
	/**
	 * 判断该分类下有没有子分类
	 * @param id
	 * @return
	 */
	@Query("from ArticleCategory c where c.parent_id= :id")
	List<ArticleCategory> getAllByCatId(@Param("id") long id);
	
	/**
	 * 判断该分类下有没有子分类   新的方法      
	 * @param id
	 * @return
	 */   
	//SELECT *,CONCAT(parentPath,',',id) as orderby FROM `cat` WHERE CONCAT(',',parentPath,',') like ',1,%' ORDER BY CONCAT(parentPath,',',id) ASC
//	@Query("select * from ArticleCategory  where CONCAT(',',parentCollection,',')  like ',:id,%' order by  CONCAT(parentCollection,',',id) ASC")
//	List<ArticleCategory> getAllByCatIdNew(@Param("id") long id);  
	//SELECT *,CONCAT(parentPath,',',id) as orderby FROM `cat` WHERE CONCAT(',',parentPath,',') like ',1,%' ORDER BY CONCAT(parentPath,',',id) ASC
	@Query("from ArticleCategory c  where CONCAT(',',c.parentCollection,',')  like CONCAT('%,',:parentCollection,',%') ORDER BY CONCAT(c.parentCollection,',',parentCollection) asc")
	List<ArticleCategory> getAllByCatIdNew(@Param("parentCollection") String parentCollection);   //order by id asc
	
	/**
	 * 当点击父类的时候，把自身包含的文章以及子类包含的文章  都查找出来
	 * @param parentCollection
	 * @return
	 */
	@Query("SELECT id from ArticleCategory c  where CONCAT(',',c.parentCollection,',')  like CONCAT('%',:parentCollection,'%')")
	List<Long> getAllArticleByParentCollection(@Param("parentCollection") String parentCollection);   //order by id asc
	
	
	
}
